﻿package com.saint.bitmapDistortion 
{
	import com.saint.utils.Logger;
	import flash.display.Bitmap;
	import flash.display.MovieClip;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;
    import org.flashsandy.display.DistortImage;
	import flash.display.DisplayObject;
	
	/**
	 * ...
	 * @author kembera
	 */
	public class BitmapDistortionTest2 extends Sprite
	{
					
				
		private var image1Width:Number = 400;
		private var image1Height:Number = 300;
		private var image2Width:Number = 400;
		private var image2Height:Number = 300;
		
		private var topLeftX:Number = 10;
		private var topLeftY:Number = 50;
	
		private var shape:Shape = new Shape();
		
		private var bitmap1:Bitmap = new Bitmap(new Goat(0, 0));
		private var bitmap2:Bitmap = new Bitmap(new Goat(0, 0));
		
		
		private var distortion1:DistortImage = new DistortImage(image1Width, image1Height, 2, 2);
		private var distortion2:DistortImage = new DistortImage(image1Width, image1Height, 2, 2);
		
		private var showGrid:Boolean = true;
		private var dragBtn1:MovieClip;
		private var dragBtn2:MovieClip;
		private var dragBtn3:MovieClip;
		private var dragBtn4:MovieClip;
		private var dragBtn5:MovieClip;
		private var dragBtn6:MovieClip;
            
		public function BitmapDistortionTest2() 
		{
			Logger.debug("BitmapDistortionTest2.BitmapDistortionTest2");	
			
			onCreationComplete();
			createButtons();
			addEventListener(Event.ENTER_FRAME, onEnterFrame);
		
			
		}
		
		private function createButtons():void 
		{
			
			dragBtn1 = new DragButton();
			dragBtn2 = new DragButton();
			dragBtn3 = new DragButton();
			dragBtn4 = new DragButton();
			dragBtn5 = new DragButton();
			dragBtn6 = new DragButton();
			
			dragBtn1.x = topLeftX;
			dragBtn1.y = topLeftY;
			
			dragBtn2.x = topLeftX + image1Width;
			dragBtn2.y = topLeftY;
			
			dragBtn3.x = topLeftX + image1Width;
			dragBtn3.y = topLeftY + image1Height;
			
			dragBtn4.x = topLeftX;
			dragBtn4.y = topLeftY + image1Height;			
			
			dragBtn5.x = topLeftX + image1Width + image2Width;
			dragBtn5.y = topLeftY;
			
			dragBtn6.x = topLeftX + image1Width + image2Width;
			dragBtn6.y = topLeftY + image2Height;
			
			dragBtn1.addEventListener(MouseEvent.MOUSE_DOWN, startButtonDrag);
			dragBtn2.addEventListener(MouseEvent.MOUSE_DOWN, startButtonDrag);
			dragBtn3.addEventListener(MouseEvent.MOUSE_DOWN, startButtonDrag);
			dragBtn4.addEventListener(MouseEvent.MOUSE_DOWN, startButtonDrag);
			dragBtn5.addEventListener(MouseEvent.MOUSE_DOWN, startButtonDrag);
			dragBtn6.addEventListener(MouseEvent.MOUSE_DOWN, startButtonDrag);
			
			dragBtn1.addEventListener(MouseEvent.MOUSE_UP, stopButtonDrag);
			dragBtn2.addEventListener(MouseEvent.MOUSE_UP, stopButtonDrag);
			dragBtn3.addEventListener(MouseEvent.MOUSE_UP, stopButtonDrag);
			dragBtn4.addEventListener(MouseEvent.MOUSE_UP, stopButtonDrag);
			dragBtn5.addEventListener(MouseEvent.MOUSE_UP, stopButtonDrag);
			dragBtn6.addEventListener(MouseEvent.MOUSE_UP, stopButtonDrag);
			
			addChild(dragBtn1);
			addChild(dragBtn2);
			addChild(dragBtn3);
			addChild(dragBtn4);
			addChild(dragBtn5);
			addChild(dragBtn6);
		}
		
		private function stopButtonDrag(e:MouseEvent):void 
		{
			e.target.stopDrag();
		}
		
		private function startButtonDrag(e:MouseEvent):void 
		{
			e.target.startDrag(false);
		}
		
		private function onCreationComplete ():void {
			this.shape.x = this.shape.y = 50;
			this.addChild(this.shape);
		}
		
		private function onEnterFrame (e:Event):void {
			this.shape.graphics.clear();
			
			if (this.showGrid){// || this.showGridCheckbox.selected){
				this.shape.graphics.lineStyle(1, 0x00FF00);
			}
			distortion1.setTransform(this.shape.graphics, 
									this.bitmap1.bitmapData, 
									this.getDisplayObjectCenter(this.dragBtn1, -50, -50), 
									this.getDisplayObjectCenter(this.dragBtn2, -50, -50), 
									this.getDisplayObjectCenter(this.dragBtn3, -50, -50), 
									this.getDisplayObjectCenter(this.dragBtn4, -50, -50));
									
			distortion2.setTransform(this.shape.graphics, 
									this.bitmap2.bitmapData, 
									this.getDisplayObjectCenter(this.dragBtn2, -50, -50), 
									this.getDisplayObjectCenter(this.dragBtn5, -50, -50), 
									this.getDisplayObjectCenter(this.dragBtn6, -50, -50), 
									this.getDisplayObjectCenter(this.dragBtn3, -50, -50));
		}
		
		
		
		private function getDisplayObjectCenter (child:DisplayObject, x:Number=0, y:Number=0):Point {
			var center:Point = new Point();
			center.x = child.x + child.width/2 + x;
			center.y = child.y + child.height/2 + y;
			return center;
		}
		
	}

}